package org.greyhawk.samonitor.web;

import java.util.ArrayList;
import java.util.List;

import org.greyhawk.samonitor.domain.Site;
import org.greyhawk.samonitor.vo.MainClass;
import org.greyhawk.samonitor.vo.SiteCheckMessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping("/site/**")
@Controller
public class SiteController {
	
	private final static Integer COUNT = 9;
	

    @RequestMapping
    public void get(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    }

    @RequestMapping(method = RequestMethod.POST)
    public void post(@PathVariable Long id, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    }
    
    

    @RequestMapping
    public ModelAndView list(HttpServletRequest request) {
    	List<Site> siteList =  Site.findAllSites();
    	ModelAndView view = new ModelAndView();
    	view.setViewName("/site/list");
    	view.addObject("siteList", siteList);
    	MainClass mainClass = new MainClass();
    	mainClass.cofig();
    	request.getSession(true).setAttribute("mainClass", mainClass);
        return view;
    }
    
    
	@RequestMapping
    public ModelAndView config(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response){
    	List<Site> sites = Site.findMainSites(0,COUNT);
    	Long size = Site.findMainSitesCount();
    	Long siteCount = new Long(0);
    	if(size != 0)
    		siteCount = size / COUNT + 1;
    	ModelAndView view = new ModelAndView();
    	view.setViewName("/site/config");
    	view.addObject("size", size);
    	view.addObject("sizeCount", siteCount);
    	view.addObject("mainSites",sites);
    	return view;
    }
	
	@RequestMapping("/site/page/{pageNum}")
	public ModelAndView page(@PathVariable("pageNum") int pageNum){
		List<Site> sites = Site.findMainSites((pageNum-1)*COUNT,pageNum*COUNT);
		Long size = Site.findMainSitesCount();
    	Long siteCount = new Long(0);
    	if(size != 0)
    		siteCount = size / COUNT + 1;
    	ModelAndView view = new ModelAndView();
    	view.setViewName("/site/config");
    	view.addObject("size", size);
    	view.addObject("sizeCount", siteCount);
    	view.addObject("mainSites",sites);
    	return view;
	}
	
	
    
    @RequestMapping(method=RequestMethod.POST)
    public String submit(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response){
    	String name = request.getParameter("name");
    	String urlPath = request.getParameter("urlPath");
    	String mainSiteId = request.getParameter("mainSiteId");
    	Site site = new Site();
    	site.setName(name);
    	site.setUrlPath(urlPath);
    	if(!mainSiteId.isEmpty()){
    		Site mainSite = Site.findSite(new Long(mainSiteId));
    		site.setMainSite(mainSite);
    		System.out.println(mainSite.getName());
    	}
    	site.persist();
    	return "redirect:/site/config";
    }
    
    @RequestMapping("/site/delete/{id}")
    public String delete(@PathVariable("id") long siteId){
    	Site site = Site.findSite(siteId);
    	site.remove();
    	return "redirect:/site/config";
    }
    
    @RequestMapping("/site/channels/{id}")
    public @ResponseBody List<Site> getChannels(@PathVariable("id") long siteId){
    	Site site = Site.findSite(siteId);
    	List<Site> list = site.getChannelList();
    	List<Site> destList = new ArrayList<Site>();
    	for (Site site2 : list) {
			Site s = new Site();
			s.setId(site2.getId());
			s.setName(site2.getName());
			s.setUrlPath(site2.getUrlPath());
			destList.add(s);
		}
    	return destList;
    }
    
    @RequestMapping(value="/site/checksite",method=RequestMethod.GET)
    public @ResponseBody SiteCheckMessage checksite(@RequestParam String urlPath){
    	SiteCheckMessage message = new SiteCheckMessage();
    	if(Site.findSitesByUrlPath(urlPath).getResultList().size() > 0){
    		message.existMessgae();
    	}
    	else{
    		message.notExistMessgae();
    	}
    	return message;
    }
    
    
    
}
